using System;
using System.Security.Cryptography;
using System.Text;

namespace OS.Common.Encrypt
{
	/// <summary>
	/// Aes加密
	/// </summary>
	public class AesRijndael
	{
		/// <summary>
		///    aes 加密
		/// </summary>
		/// <param name="toEncrypt"></param>
		/// <param name="key"></param>
		/// <param name="encoding">加密编码方式    默认为   utf-8  </param>
		/// <returns></returns>
		public static string Encrypt(string toEncrypt, string key, Encoding encoding = null)
		{
			string empty = string.Empty;
			if (string.IsNullOrEmpty(key))
			{
				throw new ArgumentNullException("key", "key值不能为空");
			}
			if (string.IsNullOrEmpty(toEncrypt))
			{
				return empty;
			}
			if (encoding == null)
			{
				encoding = Encoding.UTF8;
			}
			byte[] bytes = encoding.GetBytes(key);
			byte[] bytes2 = encoding.GetBytes(toEncrypt);
			return Convert.ToBase64String(Encrypt(bytes, bytes2));
		}

		/// <summary>
		///   加密
		/// </summary>
		/// <param name="keyArray"></param>
		/// <param name="toEncryptArray"></param>
		/// <param name="iv">偏移量</param>
		/// <param name="keySize">key大小</param>
		/// <param name="blockSize">块大小</param>
		/// <param name="cipherMode"></param>
		/// <param name="paddingMode"></param>
		/// <returns></returns>
		public static byte[] Encrypt(byte[] keyArray, byte[] toEncryptArray, byte[] iv = null, int keySize = 256, int blockSize = 128, CipherMode cipherMode = CipherMode.ECB, PaddingMode paddingMode = PaddingMode.PKCS7)
		{
			using (Aes aes = Aes.Create())
			{
				aes.KeySize = keySize;
				aes.BlockSize = blockSize;
				aes.Key = keyArray;
				if (iv != null)
				{
					aes.IV = iv;
				}
				aes.Mode = cipherMode;
				aes.Padding = paddingMode;
				return aes.CreateEncryptor().TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
			}
		}

		/// <summary>
		///    解密
		/// </summary>
		/// <param name="toDecrypt"></param>
		/// <param name="key"></param>
		/// <param name="encoding"> 编码方式 不传值默认为  utf-8 </param>
		/// <returns></returns>
		public static string Decrypt(string toDecrypt, string key, Encoding encoding = null)
		{
			string empty = string.Empty;
			if (string.IsNullOrEmpty(key))
			{
				throw new ArgumentNullException("key", "key值不能为空");
			}
			if (string.IsNullOrEmpty(toDecrypt))
			{
				return empty;
			}
			if (encoding == null)
			{
				encoding = Encoding.UTF8;
			}
			byte[] bytes = encoding.GetBytes(key);
			byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
			byte[] bytes2 = Decrypt(bytes, toEncryptArray);
			return encoding.GetString(bytes2);
		}

		/// <summary>
		/// 解密
		/// </summary>
		/// <param name="keyArray">key的字节流</param>
		/// <param name="toEncryptArray">加密串的字节流</param>
		/// <param name="iv">偏移量</param>
		/// <param name="keySize">key大小</param>
		/// <param name="blockSize">块大小</param>
		/// <param name="cipherMode"></param>
		/// <param name="paddingMode"></param>
		/// <returns></returns>
		public static byte[] Decrypt(byte[] keyArray, byte[] toEncryptArray, byte[] iv = null, int keySize = 256, int blockSize = 128, CipherMode cipherMode = CipherMode.ECB, PaddingMode paddingMode = PaddingMode.PKCS7)
		{
			using (Aes aes = Aes.Create())
			{
				aes.KeySize = keySize;
				aes.BlockSize = blockSize;
				aes.Mode = cipherMode;
				aes.Padding = paddingMode;
				aes.Key = keyArray;
				if (iv != null)
				{
					aes.IV = iv;
				}
				return aes.CreateDecryptor().TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
			}
		}
	}
}
